<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="EcServiceProviderForm">
          <a-row>
						<a-col :span="12">
							<a-form-item label="LOGO" v-bind="validateInfos.logo" id="EcServiceProviderForm-logo" name="logo">
								<j-image-upload :fileMax="0" v-model:value="formData.logo" ></j-image-upload>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="营业执照" v-bind="validateInfos.businessLicense" id="EcServiceProviderForm-businessLicense" name="businessLicense">
								<j-image-upload :fileMax="0" v-model:value="formData.businessLicense" ></j-image-upload>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="服务商名称" v-bind="validateInfos.serviceProviderName" id="EcServiceProviderForm-serviceProviderName" name="serviceProviderName">
								<a-input v-model:value="formData.serviceProviderName" placeholder="请输入服务商名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="服务商类别" v-bind="validateInfos.serviceProviderCategory" id="EcServiceProviderForm-serviceProviderCategory" name="serviceProviderCategory">
								<j-dict-select-tag v-model:value="formData.serviceProviderCategory" dictCode="service_provider_category" placeholder="请选择服务商类别"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="服务商类型" v-bind="validateInfos.serviceProviderType" id="EcServiceProviderForm-serviceProviderType" name="serviceProviderType">
								<j-dict-select-tag v-model:value="formData.serviceProviderType" dictCode="service_provider_type" placeholder="请选择服务商类型"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label=" 统一社会信用代码" v-bind="validateInfos.businessLicenseNumber" id="EcServiceProviderForm-businessLicenseNumber" name="businessLicenseNumber">
								<a-input v-model:value="formData.businessLicenseNumber" placeholder="请输入 统一社会信用代码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="归属区域" v-bind="validateInfos.region" id="EcServiceProviderForm-region" name="region">
								<j-area-linkage v-model:value="formData.region" placeholder="请输入归属区域" saveCode="region"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="负责人" v-bind="validateInfos.personInCharge" id="EcServiceProviderForm-personInCharge" name="personInCharge">
						<j-select-user v-model:value="formData.personInCharge"      allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="手机号" v-bind="validateInfos.phone" id="EcServiceProviderForm-phone" name="phone">
								<a-input v-model:value="formData.phone" placeholder="请输入手机号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="固话号码" v-bind="validateInfos.fixedLinePhoneNumber" id="EcServiceProviderForm-fixedLinePhoneNumber" name="fixedLinePhoneNumber">
								<a-input v-model:value="formData.fixedLinePhoneNumber" placeholder="请输入固话号码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="邮箱" v-bind="validateInfos.email" id="EcServiceProviderForm-email" name="email">
								<a-input v-model:value="formData.email" placeholder="请输入邮箱"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="法人代表" v-bind="validateInfos.legalRepresentative" id="EcServiceProviderForm-legalRepresentative" name="legalRepresentative">
								<a-input v-model:value="formData.legalRepresentative" placeholder="请输入法人代表"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="法人身份证正面" v-bind="validateInfos.frontSideOfIdcard" id="EcServiceProviderForm-frontSideOfIdcard" name="frontSideOfIdcard">
								<j-image-upload :fileMax="0" v-model:value="formData.frontSideOfIdcard" ></j-image-upload>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="法人身份证反面" v-bind="validateInfos.idcardBack" id="EcServiceProviderForm-idcardBack" name="idcardBack">
								<j-image-upload :fileMax="0" v-model:value="formData.idcardBack" ></j-image-upload>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="结算方式" v-bind="validateInfos.paymentMethod" id="EcServiceProviderForm-paymentMethod" name="paymentMethod">
								<j-dict-select-tag v-model:value="formData.paymentMethod" dictCode="payment_method" placeholder="请选择结算方式"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label=" 结算周期" v-bind="validateInfos.billingCycle" id="EcServiceProviderForm-billingCycle" name="billingCycle">
								<j-dict-select-tag v-model:value="formData.billingCycle" dictCode="billing_cycle" placeholder="请选择 结算周期"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="开户行" v-bind="validateInfos.bankOfDeposit" id="EcServiceProviderForm-bankOfDeposit" name="bankOfDeposit">
								<a-input v-model:value="formData.bankOfDeposit" placeholder="请输入开户行"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label=" 银行卡号" v-bind="validateInfos.bankCardNumber" id="EcServiceProviderForm-bankCardNumber" name="bankCardNumber">
								<a-input v-model:value="formData.bankCardNumber" placeholder="请输入 银行卡号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectUser from '/@/components/Form/src/jeecg/components/JSelectUser.vue';
  import JAreaLinkage from '/@/components/Form/src/jeecg/components/JAreaLinkage.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getDateByPicker, getValueType } from '/@/utils';
  import { saveOrUpdate } from '../EcServiceProvider.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    logo: '',   
    businessLicense: '',   
    serviceProviderName: '',   
    serviceProviderCategory: '',   
    serviceProviderType: '',   
    businessLicenseNumber: '',   
    region: '',   
    personInCharge: '',   
    phone: '',   
    fixedLinePhoneNumber: '',   
    email: '',   
    legalRepresentative: '',   
    frontSideOfIdcard: '',   
    idcardBack: '',   
    paymentMethod: '',   
    billingCycle: '',   
    bankOfDeposit: '',   
    bankCardNumber: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });
  //日期个性化选择
  const fieldPickers = reactive({
  });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      // 更新个性化日期选择器的值
      model[data] = getDateByPicker(model[data], fieldPickers[data]);
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
